home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / MacPNG Library 1.02 / pngMacSrc 1.02 / PNG Library 0.80 / zLib 0.95 / minigzip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-13  |  5.5 KB  |  236 lines  |  [TEXT/KAHL]

  1. /* minigzip.c -- simulate gzip using the zlib compression library
  2.  * Copyright (C) 1995 Jean-loup Gailly.
  3.  * For conditions of distribution and use, see copyright notice in zlib.h 
  4.  */
  5.  
  6. /*
  7.  * minigzip is a minimal implementation of the gzip utility. This is
  8.  * only an example of using zlib and isn't meant to replace the
  9.  * full-featured gzip. No attempt is made to deal with file systems
  10.  * limiting names to 14 or 8+3 characters, etc... Error checking is
  11.  * very limited. So use minigzip only for testing; use gzip for the
  12.  * real thing. On MSDOS, use only on file names without extension
  13.  * or in pipe mode.
  14.  */
  15.  
  16. /* $Id: minigzip.c,v 1.5 1995/05/03 17:27:11 jloup Exp $ */
  17.  
  18. #include <stdio.h>
  19. #include "zlib.h"
  20.  
  21. #ifndef __GO32__
  22. extern void exit  OF((int));
  23. #endif
  24. extern int unlink OF((const char *));
  25.  
  26. #ifdef STDC
  27. #  include <string.h>
  28. #endif
  29.  
  30. #if defined(MSDOS) || defined(OS2) || defined(WIN32)
  31. #  include <fcntl.h>
  32. #  include <io.h>
  33. #  define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
  34. #else
  35. #  define SET_BINARY_MODE(file)
  36. #endif
  37.  
  38. #ifdef VMS
  39. #  define GZ_SUFFIX "-gz"
  40. #else
  41. #  define GZ_SUFFIX ".gz"
  42. #endif
  43. #define SUFFIX_LEN sizeof(GZ_SUFFIX)
  44.  
  45. #define BUFLEN 4096
  46. #define MAX_NAME_LEN 1024
  47.  
  48. #define local static
  49. /* For MSDOS and other systems with limitation on stack size. For Unix,
  50.     #define local
  51.    works also.
  52.  */
  53.  
  54. char *prog;
  55.  
  56. void error           OF((char *msg));
  57. void gz_compress     OF((FILE   *in, gzFile out));
  58. void gz_uncompress   OF((gzFile in, FILE   *out));
  59. void file_compress   OF((char  *file));
  60. void file_uncompress OF((char  *file));
  61. int  main            OF((int argc, char *argv[]));
  62.  
  63. /* ===========================================================================
  64.  * Display error message and exit
  65.  */
  66. void error(msg)
  67.     char *msg;
  68. {
  69.     fprintf(stderr, "%s: %s\n", prog, msg);
  70.     exit(1);
  71. }
  72.  
  73. /* ===========================================================================
  74.  * Compress input to output then close both files.
  75.  */
  76. void gz_compress(in, out)
  77.     FILE   *in;
  78.     gzFile out;
  79. {
  80.     local char buf[BUFLEN];
  81.     int len;
  82.     int err;
  83.  
  84.     for (;;) {
  85.         len = fread(buf, 1, sizeof(buf), in);
  86.         if (ferror(in)) {
  87.             perror("fread");
  88.             exit(1);
  89.         }
  90.         if (len == 0) break;
  91.  
  92.         if (gzwrite(out, buf, len) != len) error(gzerror(out, &err));
  93.     }
  94.     fclose(in);
  95.     if (gzclose(out) != Z_OK) error("failed gzclose");
  96. }
  97.  
  98. /* ===========================================================================
  99.  * Uncompress input to output then close both files.
  100.  */
  101. void gz_uncompress(in, out)
  102.     gzFile in;
  103.     FILE   *out;
  104. {
  105.     local char buf[BUFLEN];
  106.     int len;
  107.     int err;
  108.  
  109.     for (;;) {
  110.         len = gzread(in, buf, sizeof(buf));
  111.         if (len < 0) error (gzerror(in, &err));
  112.         if (len == 0) break;
  113.  
  114.         if (fwrite(buf, 1, len, out) != (uInt)len) error("failed fwrite");
  115.     }
  116.     if (fclose(out)) error("failed fclose");
  117.  
  118.     if (gzclose(in) != Z_OK) error("failed gzclose");
  119. }
  120.  
  121.  
  122. /* ===========================================================================
  123.  * Compress the given file: create a corresponding .gz file and remove the
  124.  * original.
  125.  */
  126. void file_compress(file)
  127.     char  *file;
  128. {
  129.     local char outfile[MAX_NAME_LEN];
  130.     FILE  *in;
  131.     gzFile out;
  132.  
  133.     strcpy(outfile, file);
  134.     strcat(outfile, GZ_SUFFIX);
  135.  
  136.     in = fopen(file, "rb");
  137.     if (in == NULL) {
  138.         perror(file);
  139.         exit(1);
  140.     }
  141.     out = gzopen(outfile, "wb"); /* use "wb9" for maximal compression */
  142.     if (out == NULL) {
  143.         fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile);
  144.         exit(1);
  145.     }
  146.     gz_compress(in, out);
  147.  
  148.     unlink(file);
  149. }
  150.  
  151.  
  152. /* ===========================================================================
  153.  * Uncompress the given file and remove the original.
  154.  */
  155. void file_uncompress(file)
  156.     char  *file;
  157. {
  158.     local char buf[MAX_NAME_LEN];
  159.     char *infile, *outfile;
  160.     FILE  *out;
  161.     gzFile in;
  162.     int len = strlen(file);
  163.  
  164.     strcpy(buf, file);
  165.  
  166.     if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) {
  167.         infile = file;
  168.         outfile = buf;
  169.         outfile[len-3] = '\0';
  170.     } else {
  171.         outfile = file;
  172.         infile = buf;
  173.         strcat(infile, GZ_SUFFIX);
  174.     }
  175.     in = gzopen(infile, "rb");
  176.     if (in == NULL) {
  177.         fprintf(stderr, "%s: can't gzopen %s\n", prog, infile);
  178.         exit(1);
  179.     }
  180.     out = fopen(outfile, "wb");
  181.     if (out == NULL) {
  182.         perror(file);
  183.         exit(1);
  184.     }
  185.  
  186.     gz_uncompress(in, out);
  187.  
  188.     unlink(infile);
  189. }
  190.  
  191.  
  192. /* ===========================================================================
  193.  * Usage:  minigzip [-d] [files...]
  194.  */
  195.  
  196. int main(argc, argv)
  197.     int argc;
  198.     char *argv[];
  199. {
  200.     int uncompr = 0;
  201.     gzFile file;
  202.  
  203.     prog = argv[0];
  204.     argc--, argv++;
  205.  
  206.     if (argc > 0) {
  207.         uncompr = (strcmp(*argv, "-d") == 0);
  208.         if (uncompr) {
  209.             argc--, argv++;
  210.         }
  211.     }
  212.     if (argc == 0) {
  213.         SET_BINARY_MODE(stdin);
  214.         SET_BINARY_MODE(stdout);
  215.         if (uncompr) {
  216.             file = gzdopen(fileno(stdin), "rb");
  217.             if (file == NULL) error("can't gzdopen stdin");
  218.             gz_uncompress(file, stdout);
  219.         } else {
  220.             file = gzdopen(fileno(stdout), "wb"); /* "wb9" for max compr. */
  221.             if (file == NULL) error("can't gzdopen stdout");
  222.             gz_compress(stdin, file);
  223.         }
  224.     } else {
  225.         do {
  226.             if (uncompr) {
  227.                 file_uncompress(*argv);
  228.             } else {
  229.                 file_compress(*argv);
  230.             }
  231.         } while (argv++, --argc);
  232.     }
  233.     exit(0);
  234.     return 0; /* to avoid warning */
  235. }
  236.